#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/7/25 14:39
# @File           : task
# @IDE            : PyCharm
# @desc           : 辅料计划
from datetime import datetime
from typing import List, Optional, Union

from pydantic import BaseModel, ConfigDict, field_validator
from core.data_types import DatetimeStr


# 计划需求项
class NeedPlanItem(BaseModel):
    type: Optional[str] = None

    name: Optional[str] = None
    number: Optional[Union[str, float]] = None
    unit: Optional[str] = None

    remark: Optional[str] = None


# 计划清单
class NeedPlan(BaseModel):
    product_type: str  # 产品类型
    station: str  # 工位

    plan_type: Optional[str] = '辅料需求'
    remark: Optional[str] = None

    start: DatetimeStr
    end: DatetimeStr

    items: List[NeedPlanItem] = []

    create_user: Optional[str] = None
    update_user: Optional[str] = None

    @field_validator('items')
    def validate_items(cls, values):
        datas = []
        for value in values:
            if not value.name:
                continue
            datas.append(value)
        return datas


class NeedPlanSimpleOut(NeedPlan):
    model_config = ConfigDict(from_attributes=True)

    id: int
    update_datetime: DatetimeStr
    create_datetime: DatetimeStr


class NeedPlanOut(NeedPlanSimpleOut):
    model_config = ConfigDict(from_attributes=True)

    status: str = None  # 计划状态
    transport_id: Optional[str] = None
    transport_datetime: Optional[DatetimeStr] = None


class NeedPlanTrigger(BaseModel):
    method: str  # call, drop
